package com.smart.nine.spel.expr.ifthenelse;

import com.smart.nine.spel.expr.User;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * 三元操作符
 *
 * @author junrui.wang
 * @create 2019/5/15
 */
public class IfThenElseExprSample {
    public static void main(String[] args) {
        User user = new User();
        user.setName("Tom");
        user.setCredits(100);

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext(user);

        Integer credits = parser.parseExpression("Name == 'Tom' ? Credits + 10 : Credits").getValue(context, Integer.class);
        System.out.println(credits);
    }
}
